//
//  CartoonViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/12.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "CartoonViewController.h"
#import "CartoonHorizontalTableViewCell.h"
#import "CartoonVerticalTableViewCell.h"
#import "CartoonLongHorizontalTableViewCell.h"
#import "AdvertisementTableViewCell.h"
#import "AdvertiseMent2TableViewCell.h"
#import "CartoonDetailTagViewController.h"
#import "CartoonDetailLIistViewController.h"

#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
#import "NetworkTool.h"
#import "MJRefresh.h"

//轮播图
#import "CartoonCycleImageData.h"
#import "CartoonCycleImageModel.h"

//广告
#import "CartoonAdvertisementData.h"
#import "CartoonAdvertisementModel.h"


#import "CartoonBookDetailModel.h"
#import "CartoonBookDetaiData.h"
#import "CartoonBookDetai2Data.h"

#define kCartoonHorizontalTableViewCellReuse @"CartoonHorizontalTableViewCellReuse"
#define kCartoonVerticalTableViewCellReuse @"CartoonVerticalTableViewCellReuse"
#define kCartoonVerticalTableViewCellReuse1 @"CartoonVerticalTableViewCellReuse1"
#define kCartoonVerticalTableViewCellReuse2 @"CartoonVerticalTableViewCellReuse2"
#define kCartoonVerticalTableViewCellReuse3 @"CartoonVerticalTableViewCellReuse3"
#define kCartoonVerticalTableViewCellReuse4 @"CartoonVerticalTableViewCellReuse4"
#define kCartoonVerticalTableViewCellReuse5 @"CartoonVerticalTableViewCellReuse5"
#define kCartoonLongHorizontalTableViewCellReuse @"CartoonLongHorizontalTableViewCellReuse"
#define kAdvertisementTableViewCellReuse @"AdvertisementTableViewCellReuse"

#define kAdvertiseMent2TableViewCellReuse @"AdvertiseMent2TableViewCellReuse"




@interface CartoonViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIScrollView *cycleImageView;


@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)CartoonCycleImageModel *cartoonModel;

@property (nonatomic,strong)NSMutableArray *advertisementArray;
@property (nonatomic,strong)CartoonAdvertisementModel *advertisementModel;

@property (nonatomic,strong)NSMutableArray *imageUrlArray;


@property (nonatomic,strong)NSMutableArray *bookDetailArray;
@property (nonatomic,strong)CartoonBookDetailModel *bookDetailModel;
@end

@implementation CartoonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _imageUrlArray = [NSMutableArray array];
   _dataArray = [NSMutableArray array];
    _advertisementArray = [NSMutableArray array];
    _bookDetailArray  = [NSMutableArray array];
    
    
    [self handleData];
    [self handeAdvertisementData];
    [self createCycleView];

    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonHorizontalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonHorizontalTableViewCellReuse];
    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonVerticalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonVerticalTableViewCellReuse];
    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonVerticalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonVerticalTableViewCellReuse1];
    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonVerticalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonVerticalTableViewCellReuse2];
    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonVerticalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonVerticalTableViewCellReuse3];
    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonVerticalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonVerticalTableViewCellReuse4];
    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonVerticalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonVerticalTableViewCellReuse5];
    [self.tableView registerNib:[UINib nibWithNibName:@"CartoonLongHorizontalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonLongHorizontalTableViewCellReuse];
    [self.tableView registerNib:[UINib nibWithNibName:@"AdvertisementTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kAdvertisementTableViewCellReuse];
    [self.tableView registerNib:[UINib nibWithNibName:@"AdvertiseMent2TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kAdvertiseMent2TableViewCellReuse];
//    _tableView.bounces = NO;
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self handleData];
            
            [_tableView.mj_header endRefreshing];
            
        });
    }];

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 10;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //长
    if (indexPath.section == 0) {
         return SCREEN_WIDTH / 3.6 * 1.5;
    }
    //水平
    else if (indexPath.section == 1){
        return SCREEN_WIDTH / 2 * 2.4;
    }
    //广告1
    else if(indexPath.section == 2 || indexPath.section == 6 || indexPath.section == 10){
        
        return SCREEN_WIDTH / 16 *4.5;
    
    }
    //广告2
    else if (indexPath.section == 4 || indexPath.section == 8){
    
        return SCREEN_WIDTH / 3.5;
    
    
    }
    //垂直
    else{
    
        return SCREEN_WIDTH / 3 * 3;
    
    }

}

- (void)handleData{
    
    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_advertise&userid=0&type=0&ui=0&ui_id=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _cartoonModel = [CartoonCycleImageModel modelWithDic:result];
        [_dataArray addObject:_cartoonModel];
        
        for (int i = 0; i <_cartoonModel.data.count; i++) {
            [_imageUrlArray addObject:_cartoonModel.data[i].cover];
        }

        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
    
    
    
    
}

- (void)handeAdvertisementData{
//    NSString *str1 = @"http://dajiaochong.517w.com/dacu_app/app/?c=BookDetail&a=get_book_info&id=1943&ui=0&ui_id=0&userid=0";

    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_advertise&userid=0&type=3&ui=0&ui_id=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _advertisementModel = [CartoonAdvertisementModel modelWithDic:result];
        [_advertisementArray addObject:_advertisementModel];
        
        
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}


#pragma mark - 处理轮播图详情数据
- (void)handleCycleImageData{
    
   


}

- (void)createCycleView{

    _cycleImageView.contentSize = CGSizeMake(SCREEN_WIDTH, 200);
    
    // 网络加载 --- 创建带标题的图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _headerView.JH_height) delegate:self placeholderImage:[UIImage imageNamed:@"default_image"]];
    
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    
//    cycleScrollView.titlesGroup = titles;
    cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    [_cycleImageView addSubview:cycleScrollView];

    
    //         --- 模拟加载延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView.imageURLStringsGroup = _imageUrlArray;

    });
    
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
#pragma mark - 获得
    
//    //获取详情界面的list与图片,需要请求两次
//    //拼接之前的mainid
//    NSString *str1 = @"http://dajiaochong.517w.com/dacu_app/app/?c=BookDetail&a=get_chapter_list&view_type=1&id=";
//    NSString *str2 = @"&ui=0&ui_id=0&userid=0&view_type=1";
//    NSString *urlString = [[str1 stringByAppendingString:_cartoonModel.data[index].man_id]stringByAppendingString:str2];
//    
//    
//    //    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_advertise&userid=0&type=3&ui=0&ui_id=0";
//    
//    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
//    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
//        
////        _advertisementModel = [CartoonAdvertisementModel modelWithDic:result];
////        [_advertisementArray addObject:_advertisementModel];
//        _bookDetailModel = [CartoonBookDetailModel modelWithDic:result];
//        [_bookDetailArray addObject:_bookDetailModel];
    
    
//        [_tableView reloadData];
//    } andFailureBlock:^(NSError *error) {
//        
//    }];
    
    //判断是某种页面
//    if ([_cartoonModel.data[index].view_type isEqualToString:@"0"]) {
//        CartoonDetailTagViewController *tag = [[CartoonDetailTagViewController alloc] init];
//        tag.bookId = _cartoonModel.data[index].man_id;
//        [self presentViewController:tag animated:YES completion:^{
//            
//        }];
//        
//       
//    }else{
        CartoonDetailLIistViewController *list = [[CartoonDetailLIistViewController alloc] init];
        list.bookId = _cartoonModel.data[index].man_id;
        [self presentViewController:list animated:YES completion:^{
            
        }];
//
//    }

    
    NSLog(@"---点击了第%ld张图片", (long)index);
    
    
    
    
    }







- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 13;


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CartoonLongHorizontalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonLongHorizontalTableViewCellReuse];
//        cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/256.0 green:arc4random_uniform(255)/256.0 blue:arc4random_uniform(255)/256.0 alpha:1];
        
        return cell;
    }
   else if (indexPath.section == 1) {
        CartoonHorizontalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonHorizontalTableViewCellReuse];
       cell.backgroundColor = [UIColor whiteColor];
       cell.CartoonHorizontalnumOfRow = indexPath.row;
        return cell;
    }
   else if (indexPath.section == 2 || indexPath.section == 6 || indexPath.section == 10){
       AdvertisementTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kAdvertisementTableViewCellReuse];
       NSURL *url = [NSURL URLWithString:_advertisementModel.data[0].cover];
       [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
       return cell;
   }
   else if (indexPath.section == 4 || indexPath.section == 8){
       AdvertiseMent2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kAdvertiseMent2TableViewCellReuse];
   
       return cell;
   }
   
   else if(indexPath.section == 3){
       
        CartoonVerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonVerticalTableViewCellReuse];
       
       cell.CartoonVerticalNumOfRow = indexPath.section;
       
       
       
        return cell;
   }else if (indexPath.section == 5){
       CartoonVerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonVerticalTableViewCellReuse1];
       
       cell.CartoonVerticalNumOfRow = indexPath.section;
return cell;
   }
   else if (indexPath.section == 7){
       CartoonVerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonVerticalTableViewCellReuse2];
       
       cell.CartoonVerticalNumOfRow = indexPath.section;
       return cell;
   }
   else if (indexPath.section == 9){
       CartoonVerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonVerticalTableViewCellReuse3];
       
       cell.CartoonVerticalNumOfRow = indexPath.section;
       return cell;
   }
   else if (indexPath.section == 11){
       CartoonVerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonVerticalTableViewCellReuse4];
       
       cell.CartoonVerticalNumOfRow = indexPath.section;
       return cell;
   }
   else {
       CartoonVerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonVerticalTableViewCellReuse5];
       
       cell.CartoonVerticalNumOfRow = indexPath.section;
       return cell;
   }
    
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
//    view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/256.0 green:arc4random_uniform(255)/256.0 blue:arc4random_uniform(255)/256.0 alpha:1];
    view.backgroundColor = [UIColor whiteColor];
    
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"皇冠"]];
//    imageView.frame = CGRectMake(0, 0, 30, 30);
//    [view addSubview:imageView];
    
    return view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 100;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, 0, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
